/*****************************************************************************************
**  Author:jxx 2022
**  QQ:283591387
**完整文档见：http://v2.volcore.xyz/document/api 【代码生成页面ViewGrid】
**常用示例见：http://v2.volcore.xyz/document/vueDev
**后台操作见：http://v2.volcore.xyz/document/netCoreDev
*****************************************************************************************/
//此js文件是用来自定义扩展业务代码，可以扩展一些自定义页面或者重新配置生成的代码
import gridHeader   from './EQU_TOOLHead.vue'; 
import gridFooter from './EQU_TOOLgridFooter.vue';
import gridBody from './EQU_TOOLBody.vue';
let extension = {
  components: {
    //查询界面扩展组件
    gridHeader: gridHeader,
    gridBody: gridBody,
    gridFooter: gridFooter,
    //新建、编辑弹出框扩展组件
    modelHeader: '',
    modelBody: '',
    modelFooter: ''
  },
  tableAction: '', //指定某张表的权限(这里填写表名,默认不用填写)
  buttons: { view: [], box: [], detail: [] }, //扩展的按钮
  methods: {
    mounted() {
      // 每30秒刷新一次页面
      this.timer = setInterval(() => {
        // this.onRefresh();
    
   
      //  window.location.reload();
      }, 30000);
      //每3秒刷新一次页面
      this.timer = setInterval(() => {
       
        if(this.$route.path=='/EQU_TOOL_TOGETHER'){//
          this.$refs.gridFooter.PlcVeiwPlan('PlcVeiwPlan');
         
          }
   
      //  window.location.reload();
      }, 5000);
    },
    beforeDestroy() {
      // 清除定时器，避免页面销毁时定时器还在运行
      clearInterval(this.timer);
    },
    onRefresh() { // ☻◯❶ ❷ ❸
      this.refresh();},
     //下面这些方法可以保留也可以删除
    onInit() {  //框架初始化配置前，

      this.buttons.unshift({  //也可以用push或者splice方法来修改buttons数组
        name: '消息', //按钮名称
      //  icon: 'el-icon-video-pause', //按钮图标vue2版本见iview文档icon，vue3版本见element ui文档icon(注意不是element puls文档)
      icon: 'el-icon-refresh',
        type: 'primary', //按钮样式vue2版本见iview文档button，vue3版本见element ui文档button
        onClick: function () {
          
          this.$refs.gridBody.setErrorMessage();
          this.$refs.gridBody.messages3=[];
   
        }
      });
       //设置主表合计
      // this.summary = true;
      this.buttons.unshift({  //也可以用push或者splice方法来修改buttons数组
        name: '停止刷新', //按钮名称
      //  icon: 'el-icon-video-pause', //按钮图标vue2版本见iview文档icon，vue3版本见element ui文档icon(注意不是element puls文档)
      icon: 'el-icon-refresh',
        type: 'primary', //按钮样式vue2版本见iview文档button，vue3版本见element ui文档button
        onClick: function () {
          this.$Message.success(this.buttons[0].name);
          if(this.buttons[0].name=="停止刷新"){
            this.beforeDestroy();
            this.buttons[0].name="自动刷新";
       this.   beforeDestroy();
        }
        else{
          this.buttons[0].name="停止刷新";
          this.mounted();
        }
        }
      });
  
        this.columns.forEach(x => {

          if (x.field=='Identifier'||x.field=='program_seq'||x.field=='tool_name') {
            x.fixed=true//也可以设置为right,固定到最右边
        }
        if (x.field=="IsTogether"||x.field=="IsMeasure") {
          x.fixed='right'//也可以设置为right,固定到最右边
      }
      if (x.field=='TogetherSeq') {
        x.formatter = (row) => {
          if(row.TogetherSeq=="1"){
          //❶ ❷ ❸●■
            return '<span style="color: red;font-weight: bold;">❶【'+row.TogetherSeq+'】</span>'
          } if(row.TogetherSeq=="2"){
            //❶ ❷ ❸●■
              return '<span style="color: gold;font-weight: bold;">❷【'+row.TogetherSeq+'】</span>'
            } if(row.TogetherSeq=="3"){
              //❶ ❷ ❸●■
                 return '<span style="color: green;font-weight: bold;">❸【'+row.TogetherSeq+'】</span>'
              }else{
            return ''+row.TogetherSeq+''
          }
        }  
              
        x.fixed=true//也可以设置为right,固定到最右边
    }
   
    x.cellStyle = (row, rowIndex, columnIndex) => {   
      if(row.IsUse=="3"){
       
        return { background: "#3399ff", color: "#ffff" }}
        else if(row.IsChange=="3")//
        {
          return { background: "#cc9933", color: "#ffff" }
        }else if(row.IsUse=="6")//
        {
          return { background: "grey", color: "#ffff" }
        }

      if(row.TogetherSeq=="3"){
        return { background: "#33cc99", color: "#ffff" }
      }
      else if(row.TogetherSeq=="2")//
      {
        return { background: "#cc9933", color: "#ffff" }
      }else if(row.TogetherSeq=="1")//
      {
        return { background: "#993333", color: "#ffff" }
      }
      else{
       // return { background: "#33cc99", color: "#ffff" }
      } 
  }
      });
      if(this.$route.path=='/EQU_TOOL'){//

        this.columns.forEach(x => {
          if (x.field=='ExecuteTaskDiffTime') {
            x.formatter = (row) => {
              if(row.ExecuteTaskDiffTime>0){
                return ''+this.secondsToTime(row.ExecuteTaskDiffTime)+''
                } else{
                  return ''
                }
            }
            
        }      
    })
    }
    
      if(this.$route.path=='/EQU_TOOL_TOGETHER'){//
        this.table.cnName='整刀组装';
        this.table.url='/EQU_TOOL_TOGETHER/';
        this.columns.forEach(column=>{
          if (column.field=='ExecuteTaskDiffTime') {
            column.formatter = (row) => {
              if(row.ExecuteTaskDiffTime>0){
                return ''+this.secondsToTime(row.ExecuteTaskDiffTime)+''
                } else{
                  return ''
                }
            }
            
        }
          if(column.field=="time"||column.field=="SecornSeq"||column.field=="AccuracyAfterUse"){
            column.hidden=true;
          }
          if(column.field=="vendor_code"||column.field=="tool_materials"||column.field=="process_start_time"){
            column.hidden=true;
          }
          if(column.field=="process_end_time"||column.field=="process_before_tool_width"||column.field=="process_after_tool_width"){
            column.hidden=true;
          }
          if(column.field=="process_before_tool_length"||column.field=="process_after_tool_length"||column.field=="RotationalSpeed"){
            column.hidden=true;
          }
          if(column.field=="Feed"||column.field=="CoolingMethod"||column.field=="Hardness"){
            column.hidden=true;
          }
          if(column.field=="use_status"||column.field=="knife_length"||column.field=="blade_length"){
            column.hidden=true;
          }
          if(column.field=="category"||column.field=="HolderTypeId"){
            column.hidden=true;
          }
          if(column.field=="Accessorie2_Code"||column.field=="Ace2L"||column.field=="Ace2D"){
            column.hidden=true;
          }
          if(column.field=="Accessorie3_Code"||column.field=="Ace3L"||column.field=="Ace3D"){
            column.hidden=true;
          }
       
        });
        this.textInline = false;
        this.columns.push({
          field: '操作',
          title: '操作',
          width: 220,
          fixed: 'right',
          align: 'center',
          render: (h, { row, column, index }) => {           
              return h('div', { style: {display:'flex', color: '#0e84ff' } }, [
                
                h(
             
                  <el-tooltip class="item" style=" height:100px;" effect="light" content="写RFID并准备" placement="top-start">
               <span style="color:green;flex: 1 0 30%;font-size:35px; background-color: lightgrey;cursor:pointer;'margin-right':'8px';'border-bottom':'1px solid'"
               onClick={($e) => {
                if(this.$refs.gridFooter.tableData.length>0){
                  let tigger = false;
                  if(this.$refs.gridFooter.tableData[0].isWriteRfid=="False"){
                    this.$message.error('Rfid不可写');
                   return;
                  }
              
                    if(this.$refs.gridFooter.tableData[0].isUse=="True"){
                    this.$confirm(
                      "确认要写RFID【"+row.Identifier+row.program_seq+"】？",
                      "警告",
                      {
                        confirmButtonText: "确定",
                        cancelButtonText: "取消",
                        type: "warning",
                        center: true,
                      }
                    ).then(() => {
           
                      if (tigger) return;
                      tigger = true;
                   //   row.CurrentWriteValue = "True";
                      this.request('PlcToolRFID', row);

                    });
                  }else{
                    this.$message.error('使能未就绪');}
                  
                  }else{
                    this.$message.error('没有数据');
                  }
                
              
              }}
               > <i style="margin:10px" class="el-icon-video-play"> </i></span>
                     
                </el-tooltip>,        
                 
              ),
                h(
             
                  <el-tooltip class="item"  effect="light" content="只写RFID" placement="top-start">
               <span style="color:green;flex: 1 0 30%;font-size:35px;background-color:#FFF;cursor:pointer;'margin-right':'8px';'border-bottom':'1px solid'"
               onClick={($e) => {
                if(this.$refs.gridFooter.tableData.length>0){
                  let tigger = false;
                    this.$confirm(
                      "确认要只写RFID【"+row.Identifier+row.program_seq+"】？",
                      "警告",
                      {
                        confirmButtonText: "确定",
                        cancelButtonText: "取消",
                        type: "warning",
                        center: true,
                      }
                    ).then(() => {           
                      if (tigger) return;
                      tigger = true;
                      this.request('PlcToolRFIDNoSend', row);

                    });                
                  
                  }else{
                    this.$message.error('没有数据');
                  }               
              
              }}
               > <i style="margin:10px" class="el-icon-edit" > </i></span>
                     
                </el-tooltip>,        
                 
              ),
              
               
              // h(
              //   'span',
              //   {
              //     style: {
              //       cursor: 'pointer',
              //       'height': '100px',
              //       'margin-right': '8px',
              //       'border-bottom': '1px solid'
              //     },
              //     onClick: (e) => {
              //       let tigger = false;
              //         this.$confirm(
              //           "确认要获取【"+row.tool_name+"】吗？",
              //           "警告",
              //           {
              //             confirmButtonText: "确定",
              //             cancelButtonText: "取消",
              //             type: "warning",
              //             center: true,
              //           }
              //         ).then(() => {               
              //           if (tigger) return;
              //           tigger = true;
              //           row.CurrentWriteValue = "True";
              //           this.request('PlcGetAuto', {});

              //         });
              //     }
              //   },
              //   '获取'
              // ),
                h(
             
                  <el-tooltip class="item" effect="light" content="启动测量" placement="top-start">
               <span style="color:green; cursor:pointer;'margin-right':'8px';'border-bottom':'1px solid'"
               onClick={($e) => {
                if(this.$refs.gridFooter.tableData.length>0){
                  let tigger = false;
                  this.$confirm(
                    "确认要启动测量【"+row.tool_name+"】吗？",
                    "警告",
                    {
                      confirmButtonText: "确定",
                      cancelButtonText: "取消",
                      type: "warning",
                      center: true,
                    }
                  ).then(() => {
         
                    if (tigger) return;
                    tigger = true;
                    row.CurrentWriteValue = "True";
                    this.request('PlcStart', row);

                  });
                
                }
              }}
               > <i class="el-icon-thumb"> 启动</i></span>
                     
                </el-tooltip>,        
                 
              ),
              /*
                h(
                  'span',
                  {
                    style: {
                      cursor: 'pointer',
                      'margin-right': '8px',
                      'border-bottom': '1px solid'
                    },
                    onClick: (e) => {
                      let tigger = false;
                        this.$confirm(
                          "确认要退出整刀【"+row.tool_name+"】吗？",
                          "警告",
                          {
                            confirmButtonText: "确定",
                            cancelButtonText: "取消",
                            type: "warning",
                            center: true,
                          }
                        ).then(() => {
                          if (tigger) return;
                          tigger = true;
                        //  row.Status=5;//重做
                        row.CurrentWriteValue = "True";
                          this.request('PlcOutStock', row);
  
                        });
                    }
                  },
                  '退出整刀'
                ),*/
                h(
             
                  <el-tooltip class="item" effect="light" content="退出整刀（退库）" placement="top-start">
               <span style="color:green; cursor:pointer;'margin-right':'8px';'border-bottom':'1px solid'"
               onClick={($e) => {
                if(this.$refs.gridFooter.tableData.length>0){
                  let tigger = false;
                  this.$confirm(
                    "确认要退出整刀【"+row.tool_name+"】吗？",
                    "警告",
                    {
                      confirmButtonText: "确定",
                      cancelButtonText: "取消",
                      type: "warning",
                      center: true,
                    }
                  ).then(() => {
                    if (tigger) return;
                    tigger = true;
                  row.CurrentWriteValue = "True";
                    this.request('PlcOutStock', row);

                  });
                
                }
              }}
               > <i class="el-icon-shopping-cart-2"> 退库</i></span>
                     
                </el-tooltip>,        
                 
              ),
              /*
                h(
                  'span',
                  {
                    style: {
                      cursor: 'pointer',
                      'margin-right': '8px',
                      'border-bottom': '1px solid'
                    },
                    onClick: (e) => {
                      this.$refs.gridHeader.openModel1(row, column, index);                      

                    }
                  },
                  '入库'
                )  */
                  h(
             
                    <el-tooltip class="item" effect="light" content="入库整刀（入库）" placement="top-start">
                 <span style="color:green; cursor:pointer;'margin-right':'8px';'border-bottom':'1px solid'"
                 onClick={($e) => {
                  this.$refs.gridHeader.openModel1(row, column, index);  
                  
                }}
                 > <i class="el-icon-shopping-cart-full"> 入库</i></span>
                       
                  </el-tooltip>,        
                   
                ),
            
              ]);
  
          }
        });
      }
      
      this.pagination.size = 100;
        //示例：设置修改新建、编辑弹出框字段标签的长度
         this.boxOptions.labelWidth = 130;
         this.setFiexdSearchForm(true);//
    },
     //计算时间差
     calculateTimeDifference(startDate, endDate) {
      const difference = endDate.getTime() - startDate.getTime();
      const days = Math.floor(difference / (1000 * 60 * 60 * 24));
      const hours = Math.floor((difference % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
      const minutes = Math.floor((difference % (1000 * 60 * 60)) / (1000 * 60));
      const remainingSeconds = Math.floor((difference % (1000 * 60)) / 1000);
      return hours + ":" + minutes + ":" + remainingSeconds;
   //   return { days, hours, minutes, seconds };
  },
     //秒转换为时分秒
     secondsToTime(seconds) {
      const hours = Math.floor(seconds / 3600).toString().padStart(2, '0');
      const minutes = Math.floor((seconds % 3600) / 60).toString().padStart(2, '0');
      const remainingSeconds = (seconds % 60).toString().padStart(2, '0');
      
      return hours + ":" + minutes + ":" + remainingSeconds;
    },
    request(action, row) {
      let url = `A_UpperComputer/${action}`;
         this.http.post(url, row, true).then((result) => {
          if(result.status==false){
            this.$message.error(result.message);
          }else{
           this.$message.success('执行成功');
         
          }
         });
       },
    onInited() {
      //框架初始化配置后
      //如果要配置明细表,在此方法操作
      //this.detailOptions.columns.forEach(column=>{ });
      if(this.$route.path=='/EQU_TOOL_TOGETHER'){//
       // this.$refs.gridFooter.PlcVeiwPlan('PlcVeiwPlan');
        this.height = this.height - 400;
      // this.height = window.innerHeight - 260;
        }else{
          this.height = window.innerHeight - 260;
        }
     
        // this.columns.forEach(x => {
        //  if (x.field == 'tool_name') {
        //     x.summary = true;
        //  }
        // })
     // this.height = window.innerHeight - 260;
    },
    searchBefore(param) {
      //界面查询前,可以给param.wheres添加查询参数
      //返回false，则不会执行查询
      return true;
    },
    searchAfter(result) {
      //查询后，result返回的查询数据,可以在显示到表格前处理表格的值
      return true;
    },
    addBefore(formData) {
      //新建保存前formData为对象，包括明细表，可以给给表单设置值，自己输出看formData的值
      return true;
    },
    updateBefore(formData) {
      //编辑保存前formData为对象，包括明细表、删除行的Id
      return true;
    },
    rowClick({ row, column, event }) {
      //查询界面点击行事件
      // this.$refs.table.$refs.table.toggleRowSelection(row); //单击行时选中当前行;
      // if(this.$route.path=='/EQU_TOOL_TOGETHER'){//
      //   this.$refs.gridFooter.PlcVeiwPlan('PlcVeiwPlan');
      //   }
 
   

     // this.$refs.gridFooter.$refs.tableList.load({name:'MovePath_Id',value:row.MovePath_Id});
    //  this.$refs.gridFooter.mes="点击了行";
    },
    modelOpenAfter(row) {
      //点击编辑、新建按钮弹出框后，可以在此处写逻辑，如，从后台获取数据
      //(1)判断是编辑还是新建操作： this.currentAction=='Add';
      //(2)给弹出框设置默认值
      //(3)this.editFormFields.字段='xxx';
      //如果需要给下拉框设置默认值，请遍历this.editFormOptions找到字段配置对应data属性的key值
      //看不懂就把输出看：console.log(this.editFormOptions)

      //获取当前弹出框是新建还是编辑状态
      
      if(this.editFormFields.location==undefined||this.editFormFields.location==""){
        this.editFormFields.location=0;
      }
      
    }
  }
};
export default extension;
